var MyPhysicsCollider = cc.Class({
    extends: cc.PhysicsCollider,
    editor: {
        menu: CC_EDITOR && 'i18n:MAIN_MENU.component.physics/Collider/Polygon',
        requireComponent: cc.RigidBody
    },
    properties: {
        lineWidth: 10,
        points: [cc.Vec2]
    },
    _createShape: function (scale) {
        var shapes = [];

        var polys = this.points;
        for (var i = 0; i < polys.length - 1; i++) {
            var posBegin = polys[i];
            var posEnd = polys[i + 1];
            var linelen = posBegin.sub(posEnd).mag();  //sub计算两点之间的向量及其模长  mag返回该向量的长度

            var angle = Math.atan2(posEnd.y - posBegin.y, posEnd.x - posBegin.x) - Math.PI / 2;
            var midPos = posBegin.add(posEnd).mul(0.5);  //向量相加再缩放0.5倍
            var shape = new b2.PolygonShape();
            if(posBegin.equals(posEnd)){
                cc.log("posBegin and posEnd is same");
                continue;
            }

            if (shape) {
                shape.SetAsBox(this.lineWidth / 2 / 32, linelen / 2 / 32, new b2.Vec2(midPos.x / 32, midPos.y / 32), angle);
    
                shapes.push(shape);
            }
        }

        return shapes;
    }

    //引擎代码
    // _createShape: function (scale) {
    //     var shapes = [];

    //     var points = this.points;
        
    //     // check if last point equal to first point
    //     if (points.length > 0 && points[0].equals(points[points.length - 1])) {
    //         points.length -= 1;
    //     }

    //     var polys = PolygonSeparator.ConvexPartition(points);
    //     var offset = this.offset;

    //     for (var i = 0; i < polys.length; i++) {
    //         var poly = polys[i];

    //         var shape = null, vertices = [];
    //         var firstVertice = null;
            
    //         for (var j = 0, l = poly.length; j < l; j++) {
    //             if (!shape) {
    //                 shape = new b2.PolygonShape();
    //             }
    //             var p = poly[j];
    //             var x = (p.x + offset.x)/PTM_RATIO*scale.x;
    //             var y = (p.y + offset.y)/PTM_RATIO*scale.y;
    //             var v = new b2.Vec2(x, y);
    //             vertices.push( v );

    //             if (!firstVertice) {
    //                 firstVertice = v;
    //             }

    //             if (vertices.length === b2.maxPolygonVertices) {
    //                 shape.Set(vertices, vertices.length);
    //                 shapes.push(shape);

    //                 shape = null;

    //                 if (j < l - 1) {
    //                     vertices = [firstVertice, vertices[vertices.length - 1]];
    //                 }
    //             }
    //         }

    //         if (shape) {
    //             shape.Set(vertices, vertices.length);
    //             shapes.push(shape);
    //         }
    //     }

    //     return shapes;
    // }
});

module.exports = MyPhysicsCollider;